<?php
// seafish_api.php
header("Content-Type: application/json; charset=UTF-8");

require_once 'db_config.php';

$itemsPerPage = 6;
$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;

if ($page < 1) {
    $page = 1;
}

$offset = ($page - 1) * $itemsPerPage;

// Modify the SQL query to add an ORDER BY clause
$sql = "SELECT * FROM vuemodel ORDER BY id DESC LIMIT {$offset}, {$itemsPerPage}";
$result = $conn->query($sql);

$seafishData = array();

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        array_push($seafishData, array(
            "id" => $row["id"],
            "name" => $row["name"],
            "image" => $row["image"],
            "glb" => $row["glb"],
            "user" => $row["user"],
        ));
    }
}

$totalCountSql = "SELECT COUNT(*) as total_count FROM vuemodel";
$totalCountResult = $conn->query($totalCountSql);
$totalCount = $totalCountResult->fetch_assoc()["total_count"];

$response = array(
    "data" => $seafishData,
    "total_count" => $totalCount,
    "current_page" => $page,
    "items_per_page" => $itemsPerPage,
);

echo json_encode($response, JSON_UNESCAPED_UNICODE);

$conn->close();
?>
